//
//  Task_GetWsbsList.m
//  EpointFrame5
//
//  Created by AndLi on 15/4/23.
//  Copyright (c) 2015年 Epoint. All rights reserved.
//  获取网上办事事项中信息分类

#import "Task_GetWsbsList.h"
#import "WSBSItemCateListModel.h"

@implementation Task_GetWsbsList
-(void)execute
{
    
    NSString *url           = NSLocalizedString(@"Epoint_DNet_WMH", @"");
    NSString *ValidateData  = [EpointBizTools getToken];
    NSString *namespace     = NSLocalizedString(@"Epoint_DNet_WMH_NameSpace", @"");
    
    //请求接口
    NSString *method = @"GetWsbsList";
    NSString *ParasXml = @"<![CDATA[<?xml version=\"1.0\" encoding=\"gb2312\"?><paras></paras>]]>";
    
    NSString *key = [NSString stringWithFormat:@"%@",method];
    
    //请求接口
    EpointWebServiceUtil *ws = [[EpointWebServiceUtil alloc]initWithUrl:url namespace:namespace method:method];
    [ws addParameterForString:@"ValidateData" value:ValidateData];
    [ws addParameterForString:@"ParasXml" value:ParasXml];
    NSString *bs =  ws.start;
    
//    [self print:method withVali:ValidateData withXML:ParasXml withBs:bs];
    
    NSMutableArray *result = [self validateXMLWS2:bs];
    NSString *msg = [result objectAtIndex:1];
    if ([[result objectAtIndex:0]isEqualToString:@"1"]) {
        [DBFrameService setCacheValue:key value:bs];
        [self exeSuccessWithData:[Task_GetWsbsList getLists:bs]];
    }
    else
    {
//        [self userCache:key message:msg];
        [self exeFailureWithMessage:msg];
    }
    
}

-(void)userCache:(NSString *)key message:(NSString *)msg
{
    //获取缓存数据
    NSString *cacheBS = [DBFrameService getCacheValue:key];
    
    //判断是否存在缓存
    if (cacheBS.length>0) {
        [self exeSuccessWithData:[Task_GetWsbsList getLists:cacheBS]];
    }
    else
    {
        [self exeFailureWithMessage:msg];
    }
}

#pragma mark 解析XML
+(NSMutableArray *) getLists:(NSString *)result
{
    NSString *UserArea = [EpointStringUtil getXMLFormatedAttributeBsWithTag:result xmlTag:@"CateList"];
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:UserArea  options:0 error:nil];
    GDataXMLElement *rootElement = [doc rootElement];
    NSArray *arrays = [rootElement elementsForName:@"Cate"];
    NSMutableArray *dataList = [[NSMutableArray alloc]init];
    for (GDataXMLElement *arr in arrays) {
        
        WSBSItemCateListModel *f = [[WSBSItemCateListModel alloc]init];
        
        GDataXMLElement *ListName = [[arr elementsForName:@"ListName"] objectAtIndex:0];
        f.ListName = [ListName stringValue];
        
        GDataXMLElement *ListID = [[arr elementsForName:@"ListID"] objectAtIndex:0];
        f.ListID = [ListID stringValue];
        
        GDataXMLElement *isSingleInfo = [[arr elementsForName:@"isSingleInfo"] objectAtIndex:0];
        f.isSingleInfo = [isSingleInfo stringValue];
        
        [dataList addObject:f];
    }
    return dataList;
}


@end
